import VChart from '@/utils/u-charts.js';

function formatCompactNumber(number) {
  if (number < 0) {
    return "-" + formatCompactNumber(-1 * number); // updated
  }
  if (number < 1000) {
    return number;
  } else if (number >= 1000 && number < 1_000_000) {
    return (number / 1000).toFixed(1).replace(/\.0$/, "") + "K";
  } else if (number >= 1_000_000 && number < 1_000_000_000) {
    return (number / 1_000_000).toFixed(1).replace(/\.0$/, "") + "M";
  } else if (number >= 1_000_000_000 && number < 1_000_000_000_000) {
    return (number / 1_000_000_000).toFixed(1).replace(/\.0$/, "") + "B";
  } else if (number >= 1_000_000_000_000 && number < 1_000_000_000_000_000) {
    return (number / 1_000_000_000_000).toFixed(1).replace(/\.0$/, "") + "T";
  }
}

export default (data, dom) => {
  const dataValues = [];
  Object.keys(data).forEach(key => {
    for (let m = 1; m <= 12; m++) {
      dataValues.push({type: m + '月', month: key, value: data[key][m] || 0})
    }
  })
  const spec = {
    type: 'line',
    data: {
      values: dataValues
    },
    title: {
      visible: false
    },
    xField: 'type',
    yField: 'value',
    seriesField: 'month',
    legends: [{visible: true, orient: 'top'}],
    axes: [
      {
        orient: 'left',
        label: {
          formatMethod(val) {
            return `${formatCompactNumber(val)}`;
          }
        }
      }
    ]
  };

  const vchart = new VChart(spec, {dom: dom});
  vchart.renderSync();
};
